package com.ziyun.erp.modules.invoice.service.impl;

import com.ziyun.erp.common.e.DeleteEnum;
import com.ziyun.erp.modules.invoice.dao.OrderInvoiceRelevancyDao;
import com.ziyun.erp.modules.invoice.entity.OrderInvoiceRegisterVo;
import com.ziyun.erp.modules.invoice.entity.OrderInvoiceRelevancyEntity;
import com.ziyun.erp.modules.invoice.entity.OrderInvoiceVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.invoice.dao.OrderInvoiceDao;
import com.ziyun.erp.modules.invoice.entity.OrderInvoiceEntity;
import com.ziyun.erp.modules.invoice.service.OrderInvoiceService;
import org.springframework.transaction.annotation.Transactional;


@Service("orderInvoiceService")
public class OrderInvoiceServiceImpl extends ServiceImpl<OrderInvoiceDao, OrderInvoiceEntity> implements OrderInvoiceService {

    @Autowired
    private OrderInvoiceRelevancyDao orderInvoiceRelevancyDao;

    @Override
    public PageUtils queryPageOrderInvoiceRegisterList(Map<String, Object> params) {
        int totalCount = this.baseMapper.countOrderInvoiceRegister(params);

        int current = Integer.valueOf((String)params.get("page"));
        int size = Integer.valueOf((String)params.get("limit"));
        params.put("offset", (current - 1) * size);
        params.put("limit", size);
        List<OrderInvoiceRegisterVo> list = this.baseMapper.queryPageOrderInvoiceRegisterList(params);

        return new PageUtils(list, totalCount, size, current);
    }

    @Override
    public PageUtils queryPageOrderInvoiceList(Map<String, Object> params) {
        Page<OrderInvoiceVo> page = new Page<>();
        page.setCurrent(Integer.valueOf(params.get("page").toString()));
        page.setSize(Integer.valueOf(params.get("limit").toString()));

        List<OrderInvoiceVo> list = this.baseMapper.queryPageOrderInvoiceList(page, params);
        page.setRecords(list);
        return new PageUtils(page);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveInvoice(OrderInvoiceEntity orderInvoice, List<OrderInvoiceRelevancyEntity> orderInvoiceRelevancyList) {
        // 添加发票信息
        this.baseMapper.insert(orderInvoice);
        // 添加发票关联信息
        orderInvoiceRelevancyDao.saveBatch(orderInvoiceRelevancyList, orderInvoice.getOiId());

        return true;
    }

    @Override
    public boolean sumbitInvoice(Map<String, Object> updateMap, Integer oiId) {
        return this.baseMapper.updateByMap(updateMap, oiId);
    }

    @Override
    public boolean auditInvoice(Map<String, Object> updateMap, Integer oiId) {
        return this.baseMapper.updateByMap(updateMap, oiId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteInvoice(Map<String, Object> updateMap, Integer oiId) {
        // 删除发票信息
        this.baseMapper.updateByMap(updateMap, oiId);
        // 删除发票关联表信息
        updateMap.clear();
        updateMap.put("oirDel", DeleteEnum.DELETE.toInt());
        orderInvoiceRelevancyDao.updateByOirOiId(updateMap, oiId);

        return true;
    }


}
